<?php 

namespace app\seller\event;

use app\common\model\Shipping as ShippingModel;
use app\common\model\ShippingArea;
use app\common\utils\JsonService;

class ShippingEvent{

    /*
    * 物流货运列表
    */
    public static function shippingList($params){
        //$params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($shipping_id = isset($params['shipping_id'])){
                    $where[] = ['shipping_id','=',$params['shipping_id']];
            }
            if($shipping_code = isset($params['shipping_code'])){
                    $where[] = ['shipping_code','like',"%{$params['shipping_code']}%"];
            }
            if($shipping_name = isset($params['shipping_name'])){
                    $where[] = ['shipping_name','like',"%{$params['shipping_name']}%"];
            }
            if($shop_id = isset($params['shop_id'])){
                    $where[] = ['shop_id','like',"%{$params['shop_id']}%"];
            }
            if($shipping_desc = isset($params['shipping_desc'])){
                    $where[] = ['shipping_desc','like',"%{$params['shipping_desc']}%"];
            }
             $condition = [
            'where' => $where,
            'order' => ['shipping_id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
                'list_rows'=>20,
            ],
        ];
        $shipping = ShippingModel::getPageAll($condition);
        return $shipping;
    }


    /*
    * 新增物流货运
    */
    public static function createShipping($data){

        try {
            $res = ShippingModel::create($data);
            $insert = [
                'shipping_area_name'=>'全国其他地区',
                'shipping_code'     =>$res->shipping_code,
                'shop_id'           =>$data['shop_id'],
                'shipping_id'       =>$res->shipping_id,
                'is_default'        =>1,
                'config'            =>'a:4:{s:12:"first_weight";s:4:"1000";s:13:"second_weight";s:4:"2000";s:5:"money";s:2:"12";s:9:"add_money";s:1:"2";}',
            ];
            ShippingArea::create($insert);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新物流货运
    */
    public static function updateShipping($data){

        try {
            ShippingModel::updateOne($data, ['shipping_id' => $data['shipping_id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

